<?php
require './RunDbPdo.php';
require './RedisQ.php';
require './MmCache.php';
$model = new RunDbPdo();
$redis = new RedisQ();
$mem = new MmCache();
if(isset($_GET) && !empty($_GET)){
    extract($_GET);
    $user_id = isset($user_id)?$user_id:'';
    $slice = $user_id%2;
    $model->configFile = "./dbconfig/user{$slice}.config.php";
    $sql = "SELECT * FROM mm_users{$slice} WHERE user_id={$user_id}";
    $key = "{user_id:$user_id}";
    $data = $mem->get($key);
    if(empty($data)){
        $data = $model->getRow($sql);
        $mem->set($key, $data);
    }
    
}

if(isset($_POST) && !empty($_POST)){
    extract($_POST);
    $user_id = isset($user_id)?$user_id:'';
    $age = isset($age)?$age:'';
    $username = isset($username)?$username:'';
    $address = isset($address)?$address:'';
    $slice = $user_id%2;
    $model->configFile = "./dbconfig/user{$slice}.config.php";
    $sql = "UPDATE `mm_users{$slice}` SET username='{$username}',age='{$age}',address='{$address}' WHERE user_id={$user_id}";
    $resu = $model->query($sql);
    if($resu){
        //刷新缓存
        $key = "{user_id:$user_id}";
        $mem->remove($key);
        $sql = "UPDATE `mm_users` SET username='{$username}',age='{$age}',address='{$address}' WHERE user_id={$user_id}";
        $res = $redis->lpush('sql', $sql);
        if($res){
            header('location:select.php?user_id='.$user_id);die;
        }
    }
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="utf-8">
    <title>用户管理系统</title>
    <link href="./css/property-system.css" rel="stylesheet">
    <link href="./css/operate-system.css" rel="stylesheet">
        </head>
        <body>
            <main>
                <section class="content">
                    <fieldset class="form-list-32">
                        <h3 class="operate-title-1"><i></i>添加用户信息</h3>
                        <form action="edit.php" id="editFormId" method="post" autocomplete="off">
                            <input type="hidden" name='user_id' value="<?=isset($data['user_id'])?$data['user_id']:0?>">
                            <ul>
                                <li><h6>用户名：</h6><aside><input type="text" name="username" value="<?=isset($data['username'])?$data['username']:0?>" class="tbox30 tbox30-6"/></aside></li>
                                <li><h6>年龄：</h6><aside><input name="age" maxlength="11" value="<?=isset($data['age'])?$data['age']:''?>" class="tbox30 tbox30-6" type="text"></aside></li>
                                <li><h6>地址：</h6><aside><input name="address" value="<?=isset($data['address'])?$data['address']:''?>" class="tbox30 tbox30-6" type="text" ></aside></li>
                                <li class="agent-subbtn-wrap mt30px">
                                    <h6>&nbsp;</h6><aside><input class="btn-2" type="submit" value="提交"></aside>
                                </li>
                            </ul>

                        </form>
                    </fieldset>
                </section>
            </main>
    </body>
</html>


